package com.iipaas.wheel.twheel;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.thread.ExecutorBuilder;
import com.iipaas.twheel.TimingWheel;

import java.util.List;
import java.util.concurrent.ExecutorService;

/**
 * Description:
 * <p>
 * ClassName: Main
 * date: 2021/6/18 10:50 AM
 *
 * @author JohnnyChin
 * @version 1.0
 * @since JDK 1.8
 **/
public class Main {

    public static void main(String[] args) {

        TimingWheel<String> tw = TimingWheel.<String>builder()
                .ticksPerWheel(1000)
                .size(5)
                .listeners(CollectionUtil.newArrayList(new PrintExpirationListener<>()))
                .build();

        ExecutorService pool = ExecutorBuilder.create().setCorePoolSize(1).setMaxPoolSize(1).setKeepAliveTime(0).build();
        tw.start();
        pool.execute(new Runnable() {
            int i = 0;
            @Override
            @SuppressWarnings("all")
            public void run() {
                while (true) {
                    System.out.println(i++);
                    if (i == 2) {
                        tw.add("task" + 1, 1000);
                    }
                    if (i == 4) {
                        tw.add("task" + 10, 10000);
                    }
                    if (i == 20) {
                        tw.stop();
                        break;
                    }
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        break;
                    }
                }
                pool.shutdown();
                pool.shutdownNow();
            }
        });
    }
}
